/**
 * 客户端，用于向服务端发送信息
 */

#include "common.hpp"

int main()
{
    //创建共享内存
    key_t key = getKey();
    int shmid = createShm(key,SIZE);
    std::cout<<"cilent key:"<<toHEX(key)<<std::endl;
    std::cout<<"cilent shmid:"<<shmid<<std::endl;
    //开始进行进程关联
    char* start = (char*)shmat(shmid,nullptr,0);
    if((void*)start == (void*)-1)
    {
        perror("shmat");
        shmctl(shmid,IPC_RMID,nullptr);
        exit(1);
    }
    //成功
    printf("client start:%p\n",start);//打印地址看看
    std::cout<<"============="<<std::endl;
    struct shmid_ds buf;
    int n = shmctl(shmid,IPC_STAT,&buf);
    if(n == -1)
    {
        perror("shmctl");
        shmctl(shmid,IPC_RMID,nullptr);
        exit(1);
    }
    std::cout<<"buf.shm_cpid:"<<buf.shm_cpid<<std::endl;
    std::cout<<"buf.shm_perm.__key:"<<toHEX(buf.shm_perm.__key)<<std::endl;
    //释放共享内存
    shmdt(start);//去关联
    shmctl(shmid,IPC_RMID,nullptr);
    return 0;
}